package com.yc.sole.system.controller;

import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import com.yc.sole.system.biz.SequenceRuleBiz;
import com.yc.sole.system.request.SequenceRuleQueryReq;
import com.yc.sole.system.request.SequenceRuleReq;
import com.yc.sole.system.response.SequenceRuleResp;
import com.yc.sole.system.service.SequenceService;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author yizuomin
 * @date 2023/5/6 23:20
 **/
@RestController
@RequiredArgsConstructor
public class SequenceRuleController {

    private final SequenceRuleBiz sequenceRuleBiz;
    private final SequenceService sequenceService;

    /**
     * 新增
     *
     * @param sequenceRuleReq
     * @return RestResult
     */
    @PreAuth(value = "system:sequenceRule:save", description = "新增")
    @PostMapping("/api/system/sequenceRule/saveSequenceRule")
    public Result<Void> saveSequenceRule(@RequestBody @Validated(SaveGroup.class) SequenceRuleReq sequenceRuleReq) {
        sequenceRuleBiz.saveSequenceRule(sequenceRuleReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param sequenceRuleReq
     * @return RestResult
     */
    @PreAuth(value = "system:sequenceRule:update", description = "修改")
    @PostMapping("/api/system/sequenceRule/updateSequenceRuleById")
    public Result<Void> updateSequenceRuleById(@RequestBody @Validated(UpdateGroup.class) SequenceRuleReq sequenceRuleReq) {
        sequenceRuleBiz.updateSequenceRuleById(sequenceRuleReq);
        return Result.success();
    }

    /**
     * 通过id批量删除
     *
     * @param baseBatchIdsReq
     * @return RestResult
     */
    @PreAuth(value = "system:sequenceRule:delete", description = "删除")
    @PostMapping("/api/system/sequenceRule/deleteSequenceRule")
    public Result<Void> deleteSequenceRule(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseBatchIdsReq) {
        sequenceRuleBiz.deleteSequenceRuleByIds(baseBatchIdsReq.getIds());
        return Result.success();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return Result
     */
    @PreAuth(value = "system:sequenceRule:select", description = "查询")
    @PostMapping("/api/system/sequenceRule/selectSequenceRuleById/{id}")
    public Result<SequenceRuleResp> selectSequenceRuleById(@PathVariable Long id) {
        return Result.data(sequenceRuleBiz.selectSequenceRuleById(id));
    }


    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "system:sequenceRule:select", description = "查询")
    @PostMapping("/api/system/sequenceRule/selectSequenceRulePage")
    public Result<Paging<SequenceRuleResp>> selectSequenceRulePage(@RequestBody @Validated BasePageReq<SequenceRuleQueryReq> basePageReq) {
        return Result.data(sequenceRuleBiz.selectSequenceRulePage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 生成序列号
     *
     * @param key
     * @return
     */
    @PreAuth(value = "", description = "生成序列号", check = false)
    @PostMapping("/api/sequenceRule/generateSeqNo")
    public Result<String> generateSeqNo(@RequestParam @NotBlank String key) {
        String seqNo = sequenceService.generateSeqNo(key);
        return Result.data(seqNo);
    }
}
